package classesInterface;

import classesBancodeDados.FichaLaudoAutorInternHosp;
import classesBancodeDados.Paciente;
import classesBancodeDados.Pessoa;
import classesJpa.FichaLaudoAutorInternHospJpaController;
import classesJpa.exceptions.NonexistentEntityException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class TelaFichaLaudoSolicitacaoAutorizacao2 extends javax.swing.JFrame {

    /**
     * Creates new form LaudoSolicitacaoAutorizacao2
     */
    private TelaFichaLaudoSolicitacaoAutorizacao2() {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    private static TelaFichaLaudoSolicitacaoAutorizacao2 uniqueInstance = null;
    
    public static TelaFichaLaudoSolicitacaoAutorizacao2 getInstance(){
        if(uniqueInstance == null){
            uniqueInstance = new TelaFichaLaudoSolicitacaoAutorizacao2();
        }
        return uniqueInstance;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoPrincipais = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        campoJustific = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        campoResult = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        campoDiag = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        campoCidPrincipal = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        campoCidSecund = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        campoCidAssoc = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        botaoSalvar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Principais Sinais e Sintomas Clínicos");

        campoPrincipais.setColumns(20);
        campoPrincipais.setRows(5);
        jScrollPane1.setViewportView(campoPrincipais);

        jLabel2.setText("Condições que Justificam a Internação");

        campoJustific.setColumns(20);
        campoJustific.setRows(5);
        jScrollPane2.setViewportView(campoJustific);

        jLabel3.setText("Principais Resultados de Provas Diagnósticas (Exames Realizados)");

        campoResult.setColumns(20);
        campoResult.setRows(5);
        jScrollPane3.setViewportView(campoResult);

        jLabel4.setText("Diagnóstico Inicial:");

        jLabel5.setText("CID 10 Principal:");

        jLabel6.setText("CID 10 Secundário:");

        jLabel7.setText("CID 10 Causas Associadas:");

        jButton1.setText("Próximo >");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        jButton3.setText("< Anterior");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoDiag, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(campoCidPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoCidSecund, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(campoCidAssoc, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaoSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoDiag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(campoCidPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(campoCidSecund, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(campoCidAssoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(botaoSalvar)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        TelaFichaLaudoSolicitacaoAutorizacao3.getInstance().setFicha(ficha);
        TelaFichaLaudoSolicitacaoAutorizacao3.getInstance().preencherCampos(paciente, pessoa, pessoa, ficha);
        TelaFichaLaudoSolicitacaoAutorizacao3.getInstance().turnEditable(editable);
        this.setVisible(false);
        TelaFichaLaudoSolicitacaoAutorizacao3.getInstance().setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.setVisible(false);
        TelaFichaLaudoSolicitacaoAutorizacao.getInstance().setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_botaoSalvarActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        TelaFichaLaudoSolicitacaoAutorizacao.getInstance().limpaCampos();
        TelaFichaLaudoSolicitacaoAutorizacao2.getInstance().limpaCampos();
        TelaFichaLaudoSolicitacaoAutorizacao3.getInstance().limpaCampos();
        setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        TelaFichaLaudoSolicitacaoAutorizacao.getInstance().limpaCampos();
        TelaFichaLaudoSolicitacaoAutorizacao2.getInstance().limpaCampos();
        TelaFichaLaudoSolicitacaoAutorizacao3.getInstance().limpaCampos();
    }//GEN-LAST:event_formWindowClosing

    public void limpaCampos(){
        campoCidAssoc.setText("");
        campoCidPrincipal.setText("");
        campoCidSecund.setText("");
        campoDiag.setText("");
        campoJustific.setText("");
        campoPrincipais.setText("");
        campoResult.setText("");
    }
    
    public void turnEditable(boolean editable){
        this.editable = editable;
        botaoSalvar.setEnabled(editable);
        campoCidAssoc.setEditable(editable);
        campoCidPrincipal.setEditable(editable);
        campoCidSecund.setEditable(editable);
        campoDiag.setEditable(editable);
        campoJustific.setEditable(editable);
        campoPrincipais.setEditable(editable);
        campoResult.setEditable(editable);        
    }
    
    public void preencherCampos(Paciente p, FichaLaudoAutorInternHosp laih){
        paciente = p;
        this.campoCidAssoc.setText(laih.getCid10Assoc());
        this.campoCidPrincipal.setText(laih.getCid10Prim());
        this.campoCidSecund.setText(laih.getCid10Sec());
        this.campoDiag.setText(laih.getDiagnostico());
        this.campoJustific.setText(laih.getJustificativa());
        this.campoPrincipais.setText(laih.getSintomas());
        this.campoResult.setText(laih.getResultados());
    }
    
    private void salvar(){
        ficha.setCid10Assoc(this.campoCidAssoc.getText());
        ficha.setCid10Prim(this.campoCidPrincipal.getText());
        ficha.setCid10Sec(this.campoCidSecund.getText());
        ficha.setDiagnostico(this.campoDiag.getText());
        ficha.setJustificativa(this.campoJustific.getText());
        ficha.setSintomas(this.campoPrincipais.getText());
        ficha.setResultados(this.campoResult.getText());
        
        FichaLaudoAutorInternHospJpaController laic = new FichaLaudoAutorInternHospJpaController();
        try {
            laic.edit(ficha);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TelaFichaLaudoSolicitacaoAutorizacao2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TelaFichaLaudoSolicitacaoAutorizacao2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public FichaLaudoAutorInternHosp getFicha() {
        return ficha;
    }

    public void setFicha(FichaLaudoAutorInternHosp ficha) {
        this.ficha = ficha;
    }    

    public Paciente getPaciente() {
        return paciente;
    }

    public void setPaciente(Paciente paciente) {
        this.paciente = paciente;
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setPessoa(Pessoa pessoa) {
        this.pessoa = pessoa;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaFichaLaudoSolicitacaoAutorizacao2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaFichaLaudoSolicitacaoAutorizacao2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaFichaLaudoSolicitacaoAutorizacao2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaFichaLaudoSolicitacaoAutorizacao2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TelaFichaLaudoSolicitacaoAutorizacao2.getInstance().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoCidAssoc;
    private javax.swing.JTextField campoCidPrincipal;
    private javax.swing.JTextField campoCidSecund;
    private javax.swing.JTextField campoDiag;
    private javax.swing.JTextArea campoJustific;
    private javax.swing.JTextArea campoPrincipais;
    private javax.swing.JTextArea campoResult;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
    private FichaLaudoAutorInternHosp ficha;
    private boolean editable;
    private Paciente paciente;
    private Pessoa pessoa;
}
